<?php
!function_exists('adminmsg') && exit('Forbidden');
$basename="$amind_file?adminjob=hack&hackset=newinfo";
require_once(H_P.'newinfo.php');
if(!$action){
	include(D_P.'data/bbscache/newinfo_config.php');
	if (!$_POST['step']) {
		ifcheck($db_newinfoifopen,'newinfoifopen');
		$nf_position  ?  ${'position_'.$nf_position} = 'checked' : $position_1 = 'checked';
		!$nf_titlelen && $nf_titlelen = '25';
		if(!$nf_order){
			$nf_order=array(
				'pic'	=>1,
				'new'	=>2,
				'reply'	=>3,
				'dig'	=>4,
				'custom'=>0,
			);
		}
		include PrintHack('admin');exit;
	} else {
		InitGP(array('config'),'P');
		foreach($config as $key=>$value){
			if($key == 'nf_order'){
				foreach($value as $k=>$val){
					$value[$k]=(int)$val;
				}
				$value=addslashes(serialize($value));
			}else{
				$value= is_numeric($value) ? (int)$value : 7;
			}
			$db->pw_update(
				"SELECT * FROM pw_hack WHERE hk_name='$key'",
				"UPDATE pw_hack SET hk_value='$value' WHERE hk_name='$key'",
				"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('$key','$value')"
			);
		}
		$db->pw_update(
			"SELECT * FROM pw_config WHERE db_name='db_newinfoifopen'",
			"UPDATE pw_config SET db_value='$config[db_newinfoifopen]' WHERE db_name='db_newinfoifopen'",
			"INSERT INTO pw_config(db_name,db_value) VALUES ('db_newinfoifopen','$config[db_newinfoifopen]')"
		);
		updatecache_c();
		updatecache_newinfo();
		adminmsg('operate_success',$basename);
	}
} elseif ($action == 'pic') {
	if (!$_POST['step']) {
		include(D_P.'data/bbscache/newinfo_config.php');
		if(!$nf_getpic || $nf_getpic == '1'){
			$nf_getpic = '1';
			$nf_getpic_1 = 'checked';
		} else{
			$nf_getpic_2 = 'checked';
		}
		include PrintHack('admin');exit;
	} else {
		InitGP(array('nf_getpic'),'P');
		InitGP(array('urls','links','title'));
		$db->pw_update(
			"SELECT * FROM pw_hack WHERE hk_name='nf_getpic'",
			"UPDATE pw_hack SET hk_value='$nf_getpic' WHERE hk_name='nf_getpic'",
			"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_getpic','$nf_getpic')"
		);
		if ($nf_getpic==2 && is_array($urls)){
			$pic = array();
			foreach ($urls as $key => $value) {
				if($value && $links[$key]){
					$pic[] = array(
						'url'		=> stripslashes($value),
						'link'		=> stripslashes($links[$key]),
						'title'		=> stripslashes($title[$key])
					);
				}
			}
			include(D_P.'data/bbscache/newinfo_config.php');
			$nf_newinfodb['pic']=$pic;
			$nf_newinfodb = addslashes(serialize($nf_newinfodb));

			$db->pw_update(
				"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
				"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
				"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
			);
		}
		updatecache_newinfo();
		adminmsg('operate_success',"$basename&action=pic");
	}
} elseif ($action == 'custom') {
	if (!$_POST['step']) {
		include(D_P.'data/bbscache/newinfo_config.php');
		include PrintHack('admin');exit;
	} else {
		InitGP(array('nf_customtitle'),'P');
		InitGP(array('titles','links'));
		$db->pw_update(
			"SELECT * FROM pw_hack WHERE hk_name='nf_customtitle'",
			"UPDATE pw_hack SET hk_value='$nf_customtitle' WHERE hk_name='nf_customtitle'",
			"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_customtitle','$nf_customtitle')"
		);

		if (is_array($titles)){
			$custom = array();
			foreach ($titles as $key => $value) {
				if($value){
					$custom[] = array(
						'link'=>$links[$key],
						'title'=>stripslashes(Char_cv($value))
					);
				}
			}
			include(D_P.'data/bbscache/newinfo_config.php');
			$nf_newinfodb['custom']=$custom;
			$nf_newinfodb = addslashes(serialize($nf_newinfodb));
			$db->pw_update(
				"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
				"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
				"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
			);
		}
		updatecache_newinfo();
		adminmsg('operate_success',"$basename&action=custom");
	}
} elseif ($action == 'cache') {
	if (!$_GET['type']) {
		include PrintHack('admin');exit;
	}else{
		require_once(R_P.'hack/newinfo/newinfo.php');
		if($_GET['type']=='pic'){
			updatenewpic();
		}elseif($_GET['type']=='new'){
			updatenewpost();
		}elseif($_GET['type']=='reply'){
			updatenewreply();
		}elseif($_GET['type']=='dig'){
			updatedigpost();
		}
		adminmsg('operate_success',"$basename&action=cache");
	}
}

function updatenewpic(){
	global $db;
	include(D_P.'data/bbscache/newinfo_config.php');
	$newpic = $tids = array();
	$sqladd = '';
	(int)$nf_titlelen<1 && $nf_titlelen = 30;
	$fidout = getfidout();
	$fidout && $sqladd = "AND a.fid NOT IN ($fidout)";
	$query = $db->query("SELECT a.tid,a.attachurl,t.subject FROM pw_attachs a LEFT JOIN pw_threads t ON a.tid=t.tid WHERE a.type LIKE '%img' AND a.pid=0 AND t.fid!=0 $sqladd ORDER BY a.aid DESC LIMIT 0,5");
	while($rt = $db->fetch_array($query)){
		$pic=geturl($rt['attachurl'],'show');
		$pic=$pic[0] == 'imgurl' ? '' : $pic[0];
		if($pic){
			if(!in_array($rt['tid'],$tids)){
				$rt['subject']=substrs($rt['subject'],$nf_titlelen);
				$newpic[]=array(
					'url' => $pic,
					'tid' => $rt['tid'],
					'title' => $rt['subject']
				);
				$tids[]=$rt['tid'];
			}
		}
	}
	$nf_newinfodb['pic']=$newpic;
	$nf_newinfodb=addslashes(serialize($nf_newinfodb));
	$db->pw_update(
		"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
		"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
		"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
	);
	updatecache_newinfo();
}
function updatenewpost(){
	global $db;
	include(D_P.'data/bbscache/newinfo_config.php');
	$newpost=array();
	$sqladd = '';
	(int)$nf_titlelen<1 && $nf_titlelen = 30;
	empty($nf_shownum) && $nf_shownum = 7;
	$fidout = getfidout().",'0'";
	$fidout && $sqladd = " AND fid NOT IN ($fidout)";
	$query = $db->query("SELECT tid,subject FROM pw_threads WHERE ifcheck LIKE '%1' $sqladd ORDER BY tid DESC LIMIT 0,$nf_shownum");
	while($rt = $db->fetch_array($query)){
		$newpost[$rt['tid']]=substrs($rt['subject'],$nf_titlelen);
	}
	$nf_newinfodb['new']=$newpost;
	$nf_newinfodb=addslashes(serialize($nf_newinfodb));
	$db->pw_update(
		"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
		"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
		"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
	);
	updatecache_newinfo();
}
function updatenewreply(){
	global $db,$db_ptable;
	include(D_P.'data/bbscache/newinfo_config.php');
	$newreply=array();
	$tids = $sqladd = '';
	(int)$nf_titlelen<1 && $nf_titlelen = 30;
	empty($nf_shownum) && $nf_shownum = 7;
	$pw_posts = GetPtable($db_ptable);
	$fidout = getfidout().",'0'";
	$fidout && $sqladd = " AND fid NOT IN ($fidout)";
	$query = $db->query("SELECT DISTINCT tid FROM $pw_posts WHERE ifcheck LIKE '%1' $sqladd ORDER BY pid DESC LIMIT 0,$nf_shownum");
	while($rt = $db->fetch_array($query)){
		$tids && $tids.=',';
		$tids .= $rt['tid'];
		$newreply[$rt['tid']]='';
	}
	if($tids){
		$query = $db->query("SELECT tid,subject FROM pw_threads WHERE tid IN($tids)");
		while($rt = $db->fetch_array($query)){
			$newreply[$rt['tid']]=substrs('Re:'.$rt['subject'],$nf_titlelen);
		}
	}
	$nf_newinfodb['reply']=$newreply;
	$nf_newinfodb=addslashes(serialize($nf_newinfodb));
	$db->pw_update(
		"SELECT * FROM pw_hack WHERE hk_name='nf_newinfodb'",
		"UPDATE pw_hack SET hk_value='$nf_newinfodb' WHERE hk_name='nf_newinfodb'",
		"INSERT INTO pw_hack(hk_name,hk_value) VALUES ('nf_newinfodb','$nf_newinfodb')"
	);
	updatecache_newinfo();
}
?>